package br.com.faculdade.infrastructure.dao;

import br.com.faculdade.infrastructure.entity.Hospede;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.TransactionPropagationType;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.log.Log;

/**
 * Classe de acesso aos dados de Hospede
 * @author Kirmaier
 */
@AutoCreate
@Name("hospedeDAO")
@Transactional(TransactionPropagationType.SUPPORTS)
public class HospedeDAO implements Serializable {

    @In
    private Session reservaDB;
    @Logger
    private Log log;

    public void alterar(Hospede hospede) {
        log.info("Alterando o objeto no BD");

        reservaDB.saveOrUpdate(hospede);
        reservaDB.flush();
    }

    public void cancelar() {
        log.info("Cancela operacao");
        reservaDB.flush();
    }

    public void excluir(Hospede hospede) {
        log.info("Excluindo o objeto do BD");

        reservaDB.delete(hospede);
    }

    public Hospede obterPorId(Integer idhospede) {
        log.info("Buscando Hospede por ID");

        return (Hospede) reservaDB.get(Hospede.class, idhospede);
    }

    public List<Hospede> obterPorParametros(String paramNome, String paramCPF) {
        log.info("Buscando hospedes");

        Criteria c = reservaDB.createCriteria(Hospede.class);
        c.addOrder(Order.asc("nome"));

        if (paramNome != null && !paramNome.isEmpty()) {
            c.add(Restrictions.ilike("nome", paramNome, MatchMode.ANYWHERE));
        }
        if (paramCPF != null && !paramCPF.isEmpty()) {
            c.add(Restrictions.eq("cpf", paramCPF));
        }

        return c.list();
    }

    public List<Hospede> obterTodos() {
        log.info("Buscando todos hospedes");

        Criteria c = reservaDB.createCriteria(Hospede.class);
        c.addOrder(Order.asc("nome"));

        return c.list();
    }

    public void salvar(Hospede hospede) {
        log.info("Persistindo o objeto no BD", hospede.getNome());
        reservaDB.save(hospede);
        reservaDB.flush();
    }
}
